// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Base64
//
// Base64 is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Base64 is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Base64. If not, see <http://www.gnu.org/licenses/>.
//	
//// codec.h : Declaration of the Ccodec

#ifndef __CODEC_H_
#define __CODEC_H_

#include "resource.h"       // main symbols
#include <atlctl.h>

/////////////////////////////////////////////////////////////////////////////
// Ccodec
class ATL_NO_VTABLE Ccodec : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<Ccodec, &CLSID_codec>,
	public ISupportErrorInfo,
	public IDispatchImpl<Icodec, &IID_Icodec, &LIBID_BASE64Lib>,
	public IObjectSafetyImpl<Ccodec, INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA>
{
public:
	Ccodec();
	virtual ~Ccodec();

DECLARE_REGISTRY_RESOURCEID(IDR_CODEC)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(Ccodec)
	COM_INTERFACE_ENTRY(Icodec)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
	COM_INTERFACE_ENTRY(IObjectSafety)
END_COM_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

// Icodec
public:
	STDMETHOD(openFile)(BSTR sFileName);
	STDMETHOD(closeFile)();
	STDMETHOD(encodeStream)(BSTR * sTargetString);
	STDMETHOD(encodeText)(BSTR sText,/*[out,retval]*/BSTR * psRetVal);
	STDMETHOD(decodeText)(BSTR sCoded, BSTR sFilename);
	STDMETHOD(encodeFile)(BSTR sFilename,BSTR * psRetval);
private:
//	istream *m_pInput;
//	int m_nFileSize;
	HANDLE m_hFile, m_hMap;
	DWORD m_nFileSize;
	char *m_lpszDecoded;
	enum { B64_LINE_LENGTH = 76 };

	// Return the maximum number of chars required to store a decoded string translated
	// from a base 64 encoded string of length nSrcLen. Assumes CR/LF present after
	// position B64_LINE_LENGTH in each line of encoded string.
	//<param name="nSrcLen">length of encoded string</param>
	//<returns>maximum length of decoded string</returns>
	inline int Base64DecodeGetMaxRequiredLength(int nSrcLen);

	// Return the minimum number of chars required to store a decoded string translated
	// from a base 64 encoded string of length nSrcLen. Assumes no CR/LF present in the
	// encoded string.
	inline int Base64DecodeGetMinRequiredLength(int nSrcLen);
};

inline int Ccodec::Base64DecodeGetMaxRequiredLength(int nSrcLen)
{
	int nCRLF = ((nSrcLen / (B64_LINE_LENGTH + 2)) - 1) * 2;
	return ((nSrcLen - nCRLF) * 3 / 4) + nCRLF;
}

inline int Ccodec::Base64DecodeGetMinRequiredLength(int nSrcLen)
{
	return nSrcLen * 3 / 4;
}

#endif //__CODEC_H_
